package leetcode.offer;

import leetcode.base.ListNode;

/**
 * ClassName: GetKthFromEnd
 * Description: leetcode-剑指offer 22 链表中倒数第k个节点
 * date: 2021-11-08 22:28
 *
 * @author liyifan
 */
public class GetKthFromEnd {
    public ListNode getKthFromEnd(ListNode head, int k) {
        ListNode fast = head;
        ListNode slow = head;

        while (fast != null && k-- > 0)
            fast = fast.next;
        while (fast != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }
}
